import request from '@/utils/request'

type Common<T> = {
  code: string
  data: T
  mesg: string
  time: string
}

// 按条件分页查看资源
type TResource = {
  categoryId: number
  createdBy: string
  createdTime: string
  description: string
  id: number
  name: string
  operatorId: number | null
  selected: boolean
  updatedBy: string
  updatedTime: string
  url: string
}
export type TQueriedResult = {
  current: number
  hitCount?: false
  optimizeCountSql?: boolean
  orders?: any[]
  pages?: number
  records: TResource[] | []
  searchCount?: boolean
  size: number
  total: number
}
// Partial 转 为可选项
export type TCondition = Partial<{
  id: number
  name: string
  startCreateTime: string
  url: string
  categoryId: number | ''
  endCreateTime: string
  current: number
  size: number
}>

export const reqResourcePages = (condition: TCondition) => {
  return request<Common<TQueriedResult>>({
    method: 'POST',
    url: '/boss/resource/getResourcePages',
    data: condition
  })
}

// 新增或更新资源 Type Partial
export type TPResource = {
  id?: number
  name: string
  categoryId: number | string
  url: string
  description: string
}
export const reqSaveOrUpdate = (resource: TPResource) => {
  return request<Common<boolean>>({
    method: 'POST',
    url: '/boss/resource/saveOrUpdate',
    data: resource
  })
}
// 删除资源
export const deleteResource = (id: number) => {
  return request({
    method: 'DELETE',
    url: `/boss/resource/${id}`
  })
}
// 查询单个资源用于编辑
export const getResourceById = (id: number) => {
  return request<Common<TResource>>({
    method: 'GET',
    url: `/boss/resource/${id}`
  })
}
// 获取资源类别列表
export type ResourceCategoryItem = {
  createdBy: string
  createdTime: string
  description: string
  id: number
  name: string
  operatorId: number | null
  selected: boolean
  sort: number
  updatedBy: string
  updatedTime: string
}

export const getResourceCategoryAll = () => {
  return request<Common<ResourceCategoryItem[]>>({
    method: 'GET',
    url: '/boss/resource/category/getAll'
  })
}
